<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <el-form-item :label="t('examples.tencentmap.poiname')">
                        <el-input
                            type="string"
                            v-model="baTable.form.items!.poiname"
                            prop="poiname"
                            :placeholder="t('Please select field', { field: t('examples.tencentmap.poiname') })"
                            readonly
                        >
                            <template #append>
                                <el-button @click="openMapPickPop">{{ t('examples.tencentmap.choiceLocation') }}</el-button>
                            </template>
                        </el-input>
                    </el-form-item>
                    <FormItem
                        :label="t('examples.tencentmap.poiaddress')"
                        type="string"
                        v-model="baTable.form.items!.poiaddress"
                        prop="poiaddress"
                        :input-attr="{ readonly: true }"
                        :placeholder="t('Please input field', { field: t('examples.tencentmap.poiaddress') })"
                    />
                    <el-row :gutter="10">
                        <el-col :span="12">
                            <FormItem
                                :label="t('examples.tencentmap.longitude')"
                                type="string"
                                v-model="baTable.form.items!.longitude"
                                prop="longitude"
                                :input-attr="{ readonly: true }"
                                :placeholder="t('Please input field', { field: t('examples.tencentmap.longitude') })"
                            />
                        </el-col>
                        <el-col :span="12">
                            <FormItem
                                :label="t('examples.tencentmap.latitude')"
                                type="string"
                                v-model="baTable.form.items!.latitude"
                                prop="latitude"
                                :input-attr="{ readonly: true }"
                                :placeholder="t('Please input field', { field: t('examples.tencentmap.latitude') })"
                            />
                        </el-col>
                    </el-row>
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { FormInstance, FormItemRule } from 'element-plus'
import { openTencentMapPickPop } from '/@/components/xmmap/tencentMap'
import { getTencentmapKey } from '/@/api/backend/xmmap/tencentmap'
import { ElNotification } from 'element-plus'

const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass
const { t } = useI18n()
const rules: Partial<Record<string, FormItemRule[]>> = reactive({})

const state = reactive({
    tencentMapKey: '',
})
// 获取腾讯地图key，可替换自己的接口
getTencentmapKey().then((res) => {
    state.tencentMapKey = res.data
})
const openMapPickPop = () => {
    if (state.tencentMapKey == '') {
        ElNotification({
            type: 'error',
            message: '请前往【系统配置】【第三方密钥】配置腾讯地图KEY',
        })
        return false
    }
    openTencentMapPickPop(state.tencentMapKey)
}
window.addEventListener('message', function (event) {
    if (event.data.type == 'tencent-map-data') {
        const currentData = JSON.parse(event.data.data)
        console.log('腾讯地图当前位置数据：', currentData)
        baTable.form.items!.poiname = currentData.poiname
        baTable.form.items!.poiaddress = currentData.poiaddress
        baTable.form.items!.longitude = currentData.latlng.lng
        baTable.form.items!.latitude = currentData.latlng.lat
    }
})
</script>

<style scoped lang="scss"></style>
